

package projet;


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;

import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


public class FileSave extends JPanel implements ActionListener {

        /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		/**
         * @param args
         */
        static private final String newline = "\n";
        private String parentPath;
        private String fileName;
    JButton saveButton;
    JTextArea log;
    JFileChooser fc;
    
        public FileSave() {
                super(new BorderLayout());
                
        
        //creation de la fenetre
        log = new JTextArea(5,20);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);

        
        fc = new JFileChooser();
       
        //n'accepte que les dossier
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        
        saveButton = new JButton("Savegarder un fichier...");
        saveButton.addActionListener(this);
        
       
        JPanel buttonPanel = new JPanel(); 
        buttonPanel.add(saveButton);
        
        add(buttonPanel, BorderLayout.PAGE_START);
        add(logScrollPane, BorderLayout.CENTER);
                 
        }
        
         public void actionPerformed(ActionEvent e){
                 //Handle save button action.
        	 	File file;
        	 	
                 if (e.getSource() == saveButton) {
                    int returnVal = fc.showSaveDialog(FileSave.this);
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                       file = fc.getSelectedFile();
                       fileName=new String(file.getName());
                        //This is where a real application would save the file.
                        log.append("Savegarde: " + fileName + "." + newline);
                        parentPath= new String (file.getParent());
                    } else {
                        log.append("La procedure de sauvegarde a ete annulee par l'utilisateur." + newline);
                    }
                    log.setCaretPosition(log.getDocument().getLength());
               }
           Workshop w= new Workshop();
         try{  File fich= new File(parentPath,fileName);
           fich.createNewFile();
           PrintWriter texte;

           texte =  new PrintWriter(new BufferedWriter(new FileWriter(fich)));
           texte.println(w.xml());
           texte.close();}
           catch(IOException io){
        	   io.printStackTrace();
               System.out.println("IO");
               JOptionPane.showMessageDialog(this, new JTextArea("creation du fichier annule:\nUn probleme est survenu lors de la creation du fichier."), "fichier.xml",JOptionPane.PLAIN_MESSAGE);
        	   
           }
         }
                  
         
            protected static ImageIcon createImageIcon(String path) {
                java.net.URL imgURL = FileSave.class.getResource(path);
                if (imgURL != null) {
                    return new ImageIcon(imgURL);
                } else {
                    System.err.println("Fichier introuvable: " + path);
                    return null;
                }
            }

           
            private static void createAndShowGUI() {
                //fenetre
                JFrame frame = new JFrame("Sauvegarde");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               
                frame.add(new FileSave());
                
                frame.pack();
                frame.setVisible(true);
            }

            public static void main(String[] args) {
               
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        //Turn off metal's use of bold fonts
                        UIManager.put("swing.boldMetal", Boolean.FALSE); 
                        createAndShowGUI();
                    }
                });
            }

}

